﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Eg_Cookie.aspx.cs" Inherits="ASP.NET.Samples.Eg_Cookie" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Cookie</title>
    <style type="text/css">
        table,table td,table th{border:1px solid seagreen;border-collapse:collapse;}
        #Button1
        {
            width: 312px;
            height: 41px;
        }
        #btnAddCookie
        {
            width: 296px;
            height: 50px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            document.getElementById("txtDomain").value = window.location.host;
        }

        function addCookie() {
            var c_name = document.getElementById('txtCookieKey').value;
            var c_value = document.getElementById('txtCookieValue').value;
            var c_domain = document.getElementById('txtDomain').value;
            var t_date = new Date();
            t_date.setTime(t_date.getTime() + 3 * 24 * 3600 * 1000);
            var c_str = c_name + '=' + escape(c_value) + ';expires=' + t_date.toGMTString() + ';path=/';
            alert(c_str);
            document.cookie = c_str;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <label>Domain:</label><input id="txtDomain" value="" runat="server" style="width:200px; height:25px"/>
        <label>Cookie[key]:</label><input id="txtCookieKey" value="orderDate" runat="server" style="width:200px; height:25px"/>
        <label>Cookie:[value]</label><input id="txtCookieValue" runat="server" value="" style="width:200px; height:25px"/>
        <%--<asp:Button runat="server" ID="btnCreate" Width="100px" Height="30px" 
            Text="写 入" onclick="btnCreate_Click"/>--%>&nbsp;
        <input type="button" id="createCookie" style="width: 100px; height: 25px;" onclick="addCookie()"  value="写入Cookie" />
            &nbsp;
        <asp:Button runat="server" ID="btnQuery" Width="100px" Height="30px" Text="查 询" 
            onclick="btnQuery_Click"/>
        <p>Cookie个数：<strong><%=CookieCount%></strong></p>
        <table>
            <tr style="height: 30px; vertical-align: central; text-align: center; color: white; background-color: saddlebrown;">
                <td >
                    Name
                </td>
                <td >
                    Value
                </td>
                <td>
                    Domain
                </td>
                <td>Expired Date</td>
                <td>Path</td>
                <td>Secure</td>
            </tr>
            <%=CookieHtml %>
        </table>
    </div>
    </form>
    <script type="text/javascript">
        function AddCookies(name,value) {
            var Days = 1;
            var exp = new Date();
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
            document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";domain=" + window.location.host + ";path=/";
        }
    </script>
    <p>
        <input id="btnAddCookie" style="" type="button" value="Add Cookie" onclick="AddCookies('city','beijing')" /></p>
</body>
</html>
